package com.config.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.base.dao.TemplateDaoImpl;
import com.config.entity.LineConfig;

@Repository(value="lineConfigDao")
public class LineConfigDaoImpl extends TemplateDaoImpl<LineConfig,Integer> implements LineConfigDao{


	@Override
	public List<LineConfig> findAllWorkShop() {
		String sql="select * from lineConfig t where t.isLine ='N' " ;
		return jt.query(sql, ParameterizedBeanPropertyRowMapper.newInstance(LineConfig.class));
	}

	@Override
	public List<LineConfig> findLineByWorkshop(Integer workshopId) {
		StringBuffer sql = new StringBuffer(" select * from lineConfig  where fatherId=:workshopId ");
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("workshopId", workshopId);
		return jt.query(sql.toString(),params,
				ParameterizedBeanPropertyRowMapper.newInstance(LineConfig.class));
	}
	/**
	 * 获取所有能采集数据的生产线
	 * */
	@Override
	public List<LineConfig> findAllLine() {
		String sql="select * from lineConfig t where t.isLine ='Y' and t.isCollect='Y'" ;
		return jt.query(sql, ParameterizedBeanPropertyRowMapper.newInstance(LineConfig.class));
	}
  /**
    * 查询第一条生产线
    */
	@Override
	public LineConfig findFirstLine() {
		     String sql="select * from lineConfig  where isLine='Y' limit 1 ";
		     Map<String, Object> params=new HashMap<String, Object>();
			return jt.queryForObject(sql, params, ParameterizedBeanPropertyRowMapper.newInstance(LineConfig.class));
	}


	@Override
	public List<LineConfig> findByTreePage(Integer id, int start, int limit) {
		StringBuilder sb = new StringBuilder("select top "+limit+" * from (")
			.append("select row_number() over(order by id asc ) rowindex,* from ")
			.append("lineConfig where 1=1");
		Map<String, Object> params=new HashMap<String, Object>();
		if(id==null||id.equals(null)){
			sb.append(" and fatherId is NULL");
		}else{
			sb.append(" and fatherId=:id");
			params.put("id", id);
		}
		sb.append(") u where u.rowindex>:start");
		params.put("start", (start-1)*limit);
		params.put("limit", limit);
		return jt.query(sb.toString(),params,
				ParameterizedBeanPropertyRowMapper.newInstance(LineConfig.class));
	}

	@Override
	public int countByTreePage(Integer id) {
		StringBuffer sb = new StringBuffer("select count(*) from lineConfig where 1=1");
		Map<String, Object> params=new HashMap<String, Object>();
		if( id==null||id.equals(null)){
			sb.append(" and fatherId is null");
		}else{
			sb.append(" and fatherId=:id");
			params.put("id", id);
		}
		return jt.queryForObject(sb.toString(), params, Integer.class);
	
	}

	@Override
	public LineConfig getByLineNo(String lineNo) {
		StringBuffer sql=new StringBuffer("select * from lineConfig where linesNo=:lineNo");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("lineNo", lineNo);
		return jt.queryForObject(sql.toString(),params,
				ParameterizedBeanPropertyRowMapper.newInstance(LineConfig.class));
	}

}